<?php
namespace app\index\controller;
use think\Config;
use think\Db;

class Wallet extends Common
{
	/*
	public function _initialize()
	{
		session_start();
		
	}
	*/
	
	//首页
    public function index()
    {
		if(isset($_SESSION['address']))
		{
			$baseurl=Config::get("block_url");
			$url = $baseurl . "v1/wallet/index/info";
			
			$arr['address']=$_SESSION['address'];
			$info = $this->curl_request($url,$arr);
			$cinfo = json_decode($info,true);
			$uinfo = $cinfo['data'];
			$uinfo['balance'] = $uinfo['balance'] / 1000000000000000000;
			$uinfo['address'] = $_SESSION['address'];
			
			$this->assign("address",$_SESSION['address']);
			$this->assign("token",$_SESSION['token']);
			
			

			//最新交易
			$page=request()->param('page');
			if(!$page | $page<1)
			{
				$page = 1;
			}
			$url = $baseurl . "v1/wallet/index/user_message";
			$arr['page']=$page;
			$arr['limit']=20;
			$arr['address']=$_SESSION['address'];
			$info = $this->curl_request($url,$arr);
			
			//var_dump($info);exit;
			
			$cinfo = json_decode($info,true);
			foreach($cinfo['data'] AS &$c)
			{
				$c['timestamp'] = date("Y-m-d H:i:s",$c['timestamp']);
				$c['Value'] = $c['Value'] / 1000000000000000000;
				//print_r($c);
			}
			unset($c);
			
			$page_arr=array();
			$page_arr['this']=$page;
			$page_arr['next']=$page+1;
			$page_arr['pre']=$page-1;
			//$page_arr['next_last']=10;
			$page_arr['next_last']=ceil($cinfo['echo']/20);
			
			$this->assign("page",$page_arr);
			$this->assign("data",$cinfo['data']);
			
			//二维码
			//https://chart.apis.google.com/chart?chs=360x360&cht=qr&chld=L|0&chl=
			$QR_URL="https://api.qrserver.com/v1/create-qr-code/?size=360x360&data={$_SESSION['address']}";
			
			
			$this->assign("qr_url",$QR_URL);
			$this->assign("uinfo",$uinfo);
			$this->assign("action","info");
			return $this->fetch();
		}
		
		
		$this->assign("action","index");
		return $this->fetch();
    }
	
	//设置密码
	public function create()
	{
		
		$this->assign("action","create");
		return $this->fetch("index");
	}
	
	//提交请求
	public function create_prikey()
	{
		$pass1=request()->param('pass');
		$pass2=request()->param('pass2');
		
		$return_url="/index/wallet/create";
		
		if(empty($pass1) | empty($pass2))
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		$len=strlen($pass1);
		if($len<8 or $len>20)
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		
		if($pass1!=$pass2)
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		
		$baseurl=Config::get("block_url");
		$url = $baseurl . "v1/wallet/index/new";
		
		$arr['password']=$pass1;
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		
		$address = $cinfo['data']['bls'];
		$pri_key = $cinfo['data']['private_key'];
		$token = $cinfo['data']['token'];
		$cinfo['data']['password']=$pass1;
		$create_time = time();
		
		$sql="INSERT INTO address(address,pri_key,token,password,create_time) VALUES('{$address}','{$pri_key}','{$token}','{$pass1}','{$create_time}') ";
		Db::execute($sql);

		
		
		$this->assign("data",$cinfo['data']);
		$this->assign("action","create_prikey");
		
		return $this->fetch("index");
	}
	
	
	//登录
	public function login()
	{
		$address = request()->param('address');
		$password = request()->param('password');
		$token = request()->param('token');
		$pri_key = request()->param('pri_key');
		
		//私钥导入登录
		if($pri_key)
		{
			$sql = "SELECT * FROM address WHERE pri_key='{$pri_key}'";
			$info = Db::query($sql);
			
			if(!empty($info))
			{
				$_SESSION['address']=$address;
				//$_SESSION['password']=$password;
				$_SESSION['token']=$token;
				//$_SESSION['pri_key']=$pri_key;
				
				
				$this->redirect("Wallet/Index");
			}
		}
	}
	
	
	//转账
	public function send()
	{
		$amount = request()->param('amount');
		$to_address = request()->param('to_address');
		$password = request()->param('pass');
		
		$return_url ="/index/wallet";
		
		if(!$amount | !$to_address | !$password)
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		
		if(isset($_SESSION['address']))
		{
			$sql = "SELECT * FROM address WHERE address='{$_SESSION['address']}' AND password='{$password}' LIMIT 1 ";
			$info = Db::execute($sql);
			if(empty($info))
			{
				$err_msg = "Parameter Error";
				$this->err_act($err_msg,$return_url);
				return $this->fetch("index");
			}
			
			
			$baseurl=Config::get("block_url");
			$url = $baseurl . "v1/wallet/transfer/send";
			
			$arr=array();
			$arr['token'] = $_SESSION['token'];
			$arr['to'] = $to_address;
			$arr['value'] = $this->chk_num($amount,3);
			$arr['gaslimit'] = 6000000;
			
			
			//print_r($arr);exit;
			
			$info = $this->curl_request($url,$arr);
			$cinfo = json_decode($info,true);
			
			if($cinfo['code'] > 0)
			{
				$err_msg = $cinfo['echo'];
				$this->err_act($err_msg,$return_url);
				return $this->fetch("index");
			}
			
			
			$err_msg = "Success";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
			
		}
		else
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		
		
	}
	
	//导入
	public function import()
	{
		$this->assign("action","import");
		return $this->fetch("index");
	}
	
	//导入操作
	public function import_act()
	{
		$pass1 = request()->param('pass');
		$pass2 = request()->param('pass2');
		$pri_key = request()->param('pri_key');
		
		$return_url="/index/wallet/import";
		
		if(!$pass1 | !$pass2 | !$pri_key)
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		$len=strlen($pass1);
		if($len<8 or $len>20)
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		if($pass1!=$pass2)
		{
			$err_msg = "Parameter Error";
			$this->err_act($err_msg,$return_url);
			return $this->fetch("index");
		}
		
		
		
		//私钥导入登录
		if($pri_key)
		{
			$sql = "SELECT * FROM address WHERE pri_key='{$pri_key}' LIMIT 1";
			$info = Db::query($sql);
			if(!empty($info))
			{
				$address = $info[0]['address'];
				$token = $info[0]['token'];
				$sql="UPDATE address SET password={$pass1} WHERE pri_key='{$pri_key}'";
				Db::execute($sql);
				
				$_SESSION['address']=$address;
				$_SESSION['token']=$token;
				
				$this->redirect("Wallet/Index");
			}
			else
			{
				$err_msg = "Parameter Error";
				$this->err_act($err_msg,$return_url);
				return $this->fetch("index");
			}
		}
	}
	
	public function uunset()
	{
		unset($_SESSION['address']);
		unset($_SESSION['token']);
		
		$this->redirect("Wallet/Index");
	}
	
	//错误信息
	public function err_act($err_msg="Unknow Error",$return_url="/index/wallet")
	{
		$this->assign("err_msg",$err_msg);
		$this->assign("return_url",$return_url);
		$this->assign("action","error");
	}
	
	//服务协议
	public function agree()
	{
		$this->assign("action","agree");
		return $this->fetch("index");
	}	
	
	
}
